package musicstore.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import musicstore.biz.GenreBiz;
import musicstore.entity.Genre;

import org.apache.struts2.ServletActionContext;

import com.google.gson.Gson;
import com.opensymphony.xwork2.ActionSupport;

public class GenreAction extends ActionSupport {

	private String name;
	private List<Genre> genreList;

	public List<Genre> getGenreList() {
		return genreList;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	private GenreBiz genreBiz;
	public void setGenreBiz(GenreBiz genreBiz) {
		this.genreBiz = genreBiz;
	}

	public String getListByName() {
		genreList = genreBiz.getListByName(name);
		return SUCCESS;
	}

	private int id;
	private Genre genre;

	public Genre getGenre() {
		return genre;
	}
	
	public String genreGet() throws IOException{
		HttpServletResponse resp = ServletActionContext.getResponse();
		resp.setCharacterEncoding("UTF-8");
		Gson gson = new Gson();
		genre = genreBiz.getGenreById(id);
		String line = gson.toJson(genre);
		resp.getWriter().write(line);
		return null;
	}
	
	public void setGenre(Genre genre) {
		this.genre = genre;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String add() {
		if(genre.getId()>0)
			genreBiz.update(genre);
		else
			genreBiz.add(genre);
		return SUCCESS;
	}
	
	public String delete(){
		Genre g = genreBiz.getGenreById(id);
		genreBiz.delete(g);
		return SUCCESS;
		
	}
}
